package com.lilicould.controller;

import com.google.gson.Gson;

import com.lilicould.entity.ChangePasswordRequest;
import com.lilicould.entity.Users;
import com.lilicould.service.LoginService;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;


import java.io.IOException;


@WebServlet("/api/*")
public class LoginController extends BaseServlet {
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取用户输入的账号和密码
        String user_no = req.getParameter("user_no");
        String password = req.getParameter("password");
        String role = req.getParameter("role");

        // 保存用户输入的账号,即使验证失败，也保存起来，以便回显
        req.setAttribute("user_no", user_no);

        // 调用业务逻辑层进行登录验证
        LoginService loginService = new LoginService();
        HttpSession session = req.getSession();

        Users user = loginService.login(user_no, password, role);
        // 验证登录信息成功
        if (user != null) {
            session.setAttribute("userNo", user.getUserNo());
            session.setAttribute("userName", user.getName());
            // 根据角色跳转到不同的页面
            if ("student".equals(role)) {
                session.setAttribute("role", "student");
                resp.sendRedirect(req.getContextPath() + "/page/student/index.jsp");
            } else if ("teacher".equals(role)) {
                session.setAttribute("role", "teacher");
                resp.sendRedirect(req.getContextPath() + "/page/teacher/index.jsp");
            } else if ("admin".equals(role)) {
                session.setAttribute("role", "admin");
                resp.sendRedirect(req.getContextPath() + "/page/admin/index.jsp");
            } else {
                // 角色错误,跳转到登录页面
                req.setAttribute("error", "登录失败,角色错误");
                req.getRequestDispatcher("/page/login.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("error", "登录失败,请检查账号密码或身份是否选择正确");
            req.getRequestDispatcher("/page/login.jsp").forward(req, resp);
        }
    }

    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = req.getSession();
        // 销毁session对象
        session.invalidate();
        // 重定向到登录页面
        resp.sendRedirect(req.getContextPath() + "/page/login.jsp");
    }

    public void changePassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");

        ChangePasswordRequest cpr = new Gson().fromJson(req.getReader(), ChangePasswordRequest.class);

        // 调用业务逻辑层进行密码修改
        String result = new LoginService().changePassword(cpr);

        if (result != null) {
            // 若返回了错误信息
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(result);
        } else {
            // 修改成功，返回200状态码
            resp.setStatus(HttpServletResponse.SC_OK);
        }
    }

}